package com.cuitrip.util;

import android.app.Activity;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;

import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.business.webview.proxy.WebViewProxy;
import com.cuitrip.service.R;
import com.cuitrip.util.span.NoLineClickSpan;
import com.lab.rtenvironment.AppCoreRuntime;

/**
 * Created by ak on 15/9/22.
 */
public class SpanUtil {

    public static final String TITLE_TYPE = "title";
    public static final String HIGHLIGHT_TYPE = "highlights";
    public static final String TRIP_DETAIL_TYPE = "tripdetail";
    public static final String TRIP_STORY_TYPE = "tripstory";


    /**
     * 获取首部为特殊符号
     * @param text
     * @return
     */
    public static SpannableString getSizeSpan(float size, String text){
        SpannableString ss = new SpannableString(text);
        ss.setSpan(new RelativeSizeSpan(size), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(AppCoreRuntime.context.getResources().getColor(R.color.ganshi_ded8d7)), 0, text.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    /**
     * 获取颜色span
     * @param text
     * @return
     */
    public static SpannableString getColorSpanString(int color, CharSequence text){
        SpannableString ss = new SpannableString(text);
        if (text.length() != 0){
            ss.setSpan(new ForegroundColorSpan(color), 0, text.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public static SpannableString setSize(SpannableString spannableString, float size){
        spannableString.setSpan(new RelativeSizeSpan(size), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static SpannableStringBuilder getEPJump(String foreText, String colorText, final Activity activity, final String type, final String category) {
        int totalLength = foreText.length();
        int colorLength = colorText.length();
        SpannableStringBuilder builder=new SpannableStringBuilder();
        SpannableString spanString=new SpannableString(colorText);
        spanString.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                StringBuilder stringBuilder = new StringBuilder(H5UrlMaker.getEPUrl());
                stringBuilder.append("&type=").append(type)
                        .append("&category=").append(category);

                WebViewProxy.getInstance().browseWeb(activity, stringBuilder.toString(), false);
            }
        },0,colorLength,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanString.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()),0,colorLength,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.append(foreText);
        builder.append(spanString);
        return builder;
    }



}
